<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <el-form-item label="类型" prop="type">
       <el-select v-model="queryParams.type" class="flex1" placeholder="请选择类型" style="width: 100%;">
         <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
         </el-option>
       </el-select>
     </el-form-item>
     <el-form-item label="是否隐藏" prop="isHide">
       <el-select v-model="queryParams.isHide" class="flex1" placeholder="请选择是否隐藏" style="width: 100%;">
         <el-option v-for="item in hideOptions" :key="item.value" :label="item.label" :value="item.value">
         </el-option>
       </el-select>
     </el-form-item>
     <el-form-item label="是否删除" prop="isDel">
       <el-select v-model="queryParams.isDel" class="flex1" placeholder="请选择是否删除" style="width: 100%;">
         <el-option v-for="item in hideOptions" :key="item.value" :label="item.label" :value="item.value">
         </el-option>
       </el-select>
     </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
     <el-table-column label="类型" align="center" prop="type">
       <template slot-scope="scope">
         <div v-if="scope.row.type == 1">卡牌问答</div>
         <div v-if="scope.row.type == 2">骰子问答</div>
         <div v-if="scope.row.type == 3">星盘问答</div>
         <div v-if="scope.row.type == 4">六爻</div>
       </template>
     </el-table-column>
      <!-- <el-table-column label="图片" align="center" prop="imgList">
        <template slot-scope="scope">
          <div v-for="item in scope.row.imgList" style="margin-bottom: 20px;">
             <el-image
               style="width: 80px;height: 80px;"
                :src="item.img"
                :preview-src-list="[item.img]">
              </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="nameList">
        <template slot-scope="scope">
          <div v-for="item in scope.row.nameList" style="margin-top: 20px;border-bottom: 1px solid red;">
            <div v-for="item2 in item.name">{{item2.name}}:{{item2.value}}</div>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="咨询内容" align="center" prop="content" />
      <el-table-column label="打赏金额" align="center" prop="amount" />
      <el-table-column label="是否隐藏" align="center" prop="isHide">
        <template slot-scope="scope">
          <div v-if="scope.row.isHide == 0">否</div>
          <div v-if="scope.row.isHide == 1">是</div>
        </template>
      </el-table-column>

      <el-table-column label="提问时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isDel">
        <template slot-scope="scope">
          <div v-if="scope.row.isDel == 0">否</div>
          <div v-if="scope.row.isDel == 1">是</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['problem:consult:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-paperclip"
            @click="handleHide(scope.row)"
            v-hasPermi="['problem:consult:remove']"
          >{{scope.row.isHide == 0?'隐藏':'开启'}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['problem:consult:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改卡牌问题咨询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="占卜ID t_system_divine" prop="did">
          <el-input v-model="form.did" placeholder="请输入占卜ID t_system_divine" />
        </el-form-item>
        <el-form-item label="咨询内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="打赏金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入打赏金额" />
        </el-form-item>
        <el-form-item label="扩展字段" prop="extend">
          <el-input v-model="form.extend" placeholder="请输入扩展字段" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      title="提示"
      :visible.sync="answerShow"
      width="50%">
      <span>
        <el-table v-loading="loading2" :data="answerList" @selection-change="handleSelectionChange">

          <el-table-column label="昵称" align="center" prop="nickName" />
          <!-- <el-table-column label="咨询表id" align="center" prop="cid" /> -->
          <el-table-column label="语音地址" align="center" prop="voice">
            <template slot-scope="scope">
              <audio :src="scope.row.voice" controls style="width: 240px;"></audio>
            </template>
          </el-table-column>
          <el-table-column label="是否采纳" align="center" prop="isAdopt">
            <template slot-scope="scope">
              <div v-if="scope.row.isAdopt == 0">否</div>
              <div v-if="scope.row.isAdopt == 1">是</div>
            </template>
          </el-table-column>
          <el-table-column label="回复时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="answerShow = false">取 消</el-button>
        <el-button type="primary" @click="answerShow = false">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { listConsult, getConsult, delConsult,hideConsult, addConsult, updateConsult } from "@/api/problem/consult";
import { listAnswer} from "@/api/problem/answer";
export default {
  name: "Consult",
  data() {
    return {
      // 卡牌问题解答表格数据
      answerList: [],
      answerShow:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      loading2: 0,
      // 卡牌问题咨询表格数据
      consultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        type: null,
        did: null,
        content: null,
        amount: null,
        extend: null
      },
      // 表单参数
      form: {},
      // 1=卡牌问答 2=骰子问答 3=星盘问答 4=六爻
      typeOptions: [{
      	value: 1,
      	label: '卡牌问答'
      }, {
      	value: 2,
      	label: '骰子问答'
      }, {
      	value: 3,
      	label: '星盘问答'
      }, {
      	value: 4,
      	label: '六爻'
      }],
      hideOptions: [{
      	value: 0,
      	label: '否'
      }, {
      	value: 1,
      	label: '是'
      }],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询卡牌问题解答列表 */
    getanswerList(item) {
      console.log(item)
      this.loading2 = true;
      listAnswer({cid:item.id}).then(response => {
        this.answerList = response.rows;
        this.loading2 = false;
      });
    },
    /** 查询卡牌问题咨询列表 */
    getList() {
      this.loading = true;
      listConsult(this.queryParams).then(response => {
        let list = []
        let list2 = []
        response.rows.forEach(e => {
          e.divineList.forEach(k => {
            list.push({
              img:JSON.parse(k).img
            })
            list2.push({
              name: JSON.parse(k).data
            })
          })
           e.imgList = list
           e.nameList = list2
          console.log(e)
        })
        this.consultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        type: null,
        did: null,
        content: null,
        amount: null,
        createTime: null,
        extend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡牌问题咨询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.answerShow = true
      this.getanswerList(row)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConsult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？删除金额将原路返回').then(function() {
        return delConsult(ids);
      }).then((res) => {
        if(res.code != 200){
          return this.$modal.msgError(res.msg);
        }
        this.getList();
        this.$modal.msgSuccess(res.msg);
      }).catch(() => {});
    },
    handleHide(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认隐藏编号为"' + ids + '"的数据项？').then(function() {
        return hideConsult(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("隐藏成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('problem/consult/export', {
        ...this.queryParams
      }, `consult_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
